﻿using Core;
using RI_Talisman;
using RimWorld;
using RimWorld.QuestGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Security.Cryptography;
using UnityEngine;
using Verse;
using Verse.Noise;
using static HarmonyLib.Code;
using static RimWorld.ColonistBar;

namespace WhoXiuXian
{
    public class HediffComp_AncientGunHediff : HediffComp
    {
        int ticks = 0;
        int numOfGunner = 0;
        public Thing glaze;
        public Pawn pawn => base.Pawn;
        private static float MoteCastScale = 1f;

        private static Vector3 MoteCastOffset = new Vector3(0f, 0f, 0.87f);
        public override void CompPostTick(ref float severityAdjustment)
        {
            base.CompPostTick(ref severityAdjustment);
            if (ticks % 80 == 0 && pawn.Drafted)
            {
                parent.Severity = 0.09f;
                var tmpCells = GenRadial.RadialCellsAround(pawn.Position, 8, true).ToList();
                foreach (IntVec3 intVec3 in tmpCells)
                {
                    if (intVec3.GetFirstPawn(pawn.Map) != null
                        && intVec3.GetFirstPawn(pawn.Map).equipment?.Primary?.def.defName == "RI_Weapon_AncientGun")
                    {
                        numOfGunner++;
                    }
                }
                for (int x = 0; x < numOfGunner; x++)
                {
                    parent.Severity = parent.Severity + 0.1f;
                }
                numOfGunner = 0;
            }
            if (parent.Severity > 0.6f)
            {
                MoteMaker.MakeAttachedOverlay(pawn, DefDatabase<ThingDef>.GetNamed("RI_Mote_AncientGun"), MoteCastOffset, MoteCastScale);
            }
            ticks++;
        }
        public override void CompExposeData()
        {
            base.CompExposeData();
        }

    }
}